ship = {}
ship.x = 120
ship.y = 164
i = 0
tir = 0
tirmax = 0
tirs = Image.load ("tir.png", RAM)
Image.rotateDegree(tirs, 270, Image.width(tirs)/2, Image.height(tirs)/2)
vaisseau = Image.load ("ship.png", RAM)
tirx = {}
tiry = {}
tirx.tir = {}
tiry.tir = {}
counter = 0
status = 1

function fire()
i = i + 1
tirmax = tirmax + 1
tirx.tir[i] = ship.x
tiry.tir[i] = ship.y

if status >= 2 then
  i = i + 1
  tirmax = tirmax + 1
  tirx.tir[i] = (ship.x + 8)
  tiry.tir[i] = (ship.y)
  tirx.tir[(i-1)] = (ship.x - 8) 
end

if status >= 3 then
  i = i + 1
  tirmax = tirmax + 1
  tirx.tir[i] = (ship.x + 16)
  tiry.tir[i] = (ship.y)
  tirx.tir[(i-1)] = (ship.x - 16) 
  tirx.tir[(i-2)] = (ship.x) 
end

if status >= 4 then
  i = i + 1
  tirmax = tirmax + 1
  tirx.tir[i] = (ship.x + 24)
  tiry.tir[i] = (ship.y)
  tirx.tir[(i-1)] = (ship.x + 8) 
  tirx.tir[(i-2)] = (ship.x - 8) 
  tirx.tir[(i-3)] = (ship.x - 24) 
end


end

while not Keys.newPress.Start do
Controls.read ()

blub = 0

if Keys.held.Left then
ship.x = ship.x - 2
end

if Keys.held.Right then
ship.x = ship.x + 2
end

if Keys.held.Up then
ship.y = ship.y - 2
end

if Keys.held.Down then
ship.y = ship.y + 2
end

if Keys.newPress.B then
status = status + 1
  if status > 4 then
  status = 1
  end
end

if Keys.held.L or Keys.held.R then
tir = tir + 1
if tir == 10 then
fire()
tir = 0
counter = counter + 1
end
end

startDrawing()
if i > 0 then
    for i = 1, tirmax do
        tiry.tir[i] = tiry.tir[i] - 4
        if tiry.tir[i] > -8 then
            screen.blit (SCREEN_DOWN, tirx.tir[i], tiry.tir[i], tirs)
        end
        if tiry.tir[i] < -8 then
            screen.blit (SCREEN_UP, tirx.tir[i], tiry.tir[i]+192, tirs)
        end
         if tiry.tir[i] < -270 then
          -- i = i - 1
          -- tir = tir - 1
          -- tirmax = tirmax - 1
          blub = i
        end       
    end
end

if blub > 0 then
tirmax = tirmax - 1
i = i - 1
          table.remove(tiry.tir,blub)
          table.remove(tirx.tir,blub)
end
if ship.x < 0 then
    ship.x = 0
end

if ship.x > 240 then
    ship.x = 240
end

if ship.y < -196 then
    ship.y = -196
end

if ship.y > 164 then
    ship.y = 164
end

if ship.y > -32 then
screen.blit (SCREEN_DOWN, ship.x, ship.y, vaisseau)
end
if ship.y < -1 then
screen.blit (SCREEN_UP, ship.x, ship.y+192, vaisseau)
end
if Keys.held.Select then
screen.print (SCREEN_UP, 0, 0, "tir numero: "..i)
screen.print (SCREEN_UP, 0, 8, "tir total: "..tirmax)
screen.print (SCREEN_UP, 0, 16, "vaisseau x :"..ship.x.."  vaisseau y :"..ship.y)
screen.print (SCREEN_UP, 0, 24, "FPS: "..NB_FPS)
screen.print (SCREEN_UP, 0, 34, "elemente:X "..table.getn(tirx.tir).."  Y "..table.getn(tiry.tir))
screen.print (SCREEN_UP, 0, 44, "shoots: "..counter)
screen.print (SCREEN_UP, 0, 54, "status: "..status)

end
stopDrawing()
end

ship = nil
Image.destroy(vaisseau)
vaisseau = nil
Image.destroy(tirs)
tirs = nil
i = nil
tir = nil
tirmax = nil
tirx = nil
tiry = nil
